General file operation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## show parameter or method
dir("")

## argv number and value
import sys
len(sys.argv) //count
for i in sys.argv: //list

## list all file in dir
from os import listdir
print listdir(".")

## check file exists
if os.path.exists(file_name):

## look for fileName
import os
from os import walk
for (root, dirs, files) in walk("/Users/soeasyright"):
for file in files:
if file.endswith('.py'):
print(os.path.join(root, file))

## Rename FileName
os.rename(fullpath, fullpath.replace('E', 'e'))

## open file
-- type1 --
with open(file_name) as f: //no needs file.close

-- type2 --
wopen = open(file_name,'w') //w,w+
wopen.write(tmp_word)
wopen.close()

## try except
try:
print 'deal something'
raise EOFError('XD')
print 'ok'
except EOFError as e
print e.args ## XD
type, message, traceback = sys.exc_info()
print type ##<type 'exceptions.EOFError'>
print message ##XD
finally:
print 'finally'


## exit python
import sys
sys.exit(0)
sys.exit(1) //something error